將重要資訊放到yaml內
config.yaml(放在BASE_DIR)
---
email:
EMAIL_BACKEND: 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER: 'yourgmail@gmail.com'
EMAIL_HOST_PASSWORD: 'asdfasdfasdfffa'
SECRET_KEY: 'django-insecure-)33#f@1n86$lv19uz*u!9*rsb99p1&1ncnbsadfasdfo#qd@*u'
...
然後在setting內導入
如此一來可以保護機密資訊不會上傳到git
還有可以快速切換develop & production 環境
docsystem_5/settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
with open(BASE_DIR/"config.yaml","r") as f:
config = yaml.full_load(f)
dict_email_config = config.get('email','')
secret_key = config.get('SECRET_KEY','')
email_host_user = dict_email_config['EMAIL_HOST_USER']
email_host_password = dict_email_config['EMAIL_HOST_PASSWORD']
SECRET_KEY = secret_key
EMAIL_HOST_USER = email_host_user
EMAIL_HOST_PASSWORD = email_host_password
yaml的規則
--- # 表示yaml開始
# 字典 key:value
animal: pets
plant:
tree
# 數組
animal:
- Cat
- Dog
- Goldfish
# 純量
str: This_is_a_line #純文字可以不用引號
str: 'content: a string'
s1: 'content:\n a string' # 會轉換成換行
s2: "content:\n a string" # 不會轉換
str: 'labor''s day' # 兩個單引號會轉換成單引號
int_value: 314
float_value: 3.14
parent: ~ # 表示null
iso8601: 2018-12-14t21:59:43.10-05:00
date: 1976-07-31
# 換行符保留
this: |
Foo
Bar
that: >
Foo
Bar
{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }
... # 表示yaml結束